<?php

namespace addon\hotel\api\controller;

use addon\hotel\model\HotelSettlement as HotelSettlementModel;
use addon\hotel\model\OrderCommon;
use addon\supermember\model\MemberLevelOrder;
use app\api\controller\BaseApi;
use addon\hotel\model\Order as OrderModel;
use addon\hotel\model\OrderCommon as OrderCommonModel;
use Carbon\Carbon;

class Order extends BaseApi
{

    //订单列表
    public function lists()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);
        $order_common_model = new OrderCommonModel();
        $condition = array (
            [ "member_id", "=", $this->member_id ],
        );
        $order_status = isset($this->params[ 'order_status' ]) ? $this->params[ 'order_status' ] : 'all';
        switch ( $order_status ) {
            case "waitpay"://待付款
                $condition[] = [ "order_status", "=", 0 ];
                break;
            case "waitsettled"://待入驻
                $condition[] = [ "order_status", "=", 1 ];
                break;
            case "enter"://已入驻
                $condition[] = [ "order_status", "=", 4 ];
                break;
            case "complete"://已完成
                $condition[] = [ "order_status", "=", 10 ];
                break;
        }
//		if (c !== "all") {
//			$condition[] = [ "order_status", "=", $order_status ];
//		}
        $page_index = isset($this->params[ 'page' ]) ? $this->params[ 'page' ] : 1;
        $page_size = isset($this->params[ 'page_size' ]) ? $this->params[ 'page_size' ] : PAGE_LIST_ROWS;
        $res = $order_common_model->getMemberOrderPageList($condition, $page_index, $page_size, "create_time desc");
        return $this->response($res);
    }


    /**
     * 关闭订单
     */
    public function close()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        $order_id = isset($this->params[ 'order_id' ]) ? $this->params[ 'order_id' ] : 0;
        if (empty($order_id)) {
            return $this->response($this->error('', 'REQUEST_ORDER_ID'));
        }
        $order_model = new OrderCommonModel();
        $close_condition = array (
            [ 'order_id', '=', $order_id ],
            [ 'member_id', '=', $this->member_id ]
        );
        $result = $order_model->orderClose($close_condition);
        return $this->response($result);
    }

    /**
     * 获取订单数量
     */
    public function num()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);

        if (empty($this->params[ 'order_status' ])) {
            return $this->response($this->error('', 'REQUEST_ORDER_STATUS'));
        }

        $order_common_model = new OrderCommonModel();

        $data = [];
        foreach (explode(',', $this->params[ 'order_status' ]) as $order_status) {
            $condition = array (
                [ "member_id", "=", $this->member_id ],
            );
            switch ( $order_status ) {
                case "waitpay"://待付款
                    $condition[] = [ "order_status", "=", 0 ];
                    break;
                case "waitsettled"://待入驻
                    $condition[] = [ "order_status", "=", 1 ];
                    break;
                case "complete"://已完成
                    $condition[] = [ "order_status", "=", 10 ];
                    break;
            }

            $result = $order_common_model->getOrderCount($condition);
            $data[ $order_status ] = $result[ 'data' ];

        }
        return $this->response(success(0, '', $data));
    }

    public function detail()
    {
        $token = $this->checkToken();
        if ($token[ 'code' ] < 0) return $this->response($token);
        $order_common_model = new OrderCommonModel();
        $order_id = isset($this->params[ 'order_id' ]) ? $this->params[ 'order_id' ] : 0;
        $result = $order_common_model->getMemberOrderDetail($order_id, $this->member_id);
        return $this->response($result);
    }

    //线下支付
    public function offlinepay()
    {
        $out_trade_no = isset($this->params[ 'out_trade_no' ]) ? $this->params[ 'out_trade_no' ] : '';
        $order_common_model = new OrderCommonModel();
        $result  = $order_common_model->orderOfflinePay($out_trade_no);
        return $this->response($result);
    }
}